package cn.fanqinghong.basic.service.impl;

import cn.fanqinghong.basic.mapper.BaseMapper;
import cn.fanqinghong.basic.query.BaseQuery;
import cn.fanqinghong.basic.service.IBaseService;
import cn.fanqinghong.basic.query.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * FAN
 * 2022/6/8  15:39
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void removeById(Long id) {
        mapper.deleteById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    /**
     * 分页查询+高级查询
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询分页总条数
        Long total = mapper.queryCount(query);
        //查询当前页数据
        List<T> rows = mapper.queryData(query);
        return new PageList<>(total,rows);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void batch(List<Long> ids) {
            mapper.batch(ids);
    }
}
